// 定义一个数值和字符串的联合类型
let objNum : (number | string )[] = [1,2,'1','2']
console.log(objNum);

// 定义一个类型断言
let dyarr : number[] = [1,2,3,4];
let dy : number =  (dyarr as number[]).length;

console.log(dy);

// 定义一个 void 实现函数的无返回值
function fnc () :void {
    console.log('努力加油敲代码');
}
fnc();

// 定义一个 never 的函数
function infiniteLoop(): never {
    while (true) {
    }
}

// 分别定义 es5 和 ts 的函数定义
//  对应于ES5中的函数声明法定义方式，ts中需要定义函数的返回值类型

// 定义函数时， 分别实现参数类型、剩余参数、默认参数
// 可选参数 ?   代表可有可无
function fnkx(a:number,b?:number){
    if(b){
        return console.log(a+b);
    }else{
        return console.log(a);
    }
}
fnkx(1)
//参数类型
function fn(a:number,b:number){
    return console.log(a+b);
}
fn(1,2)
// 默认参数
function fn1(a1:string ,b1 = 'b1'){
    return console.log(a1+' '+b1); 
}
fn1('a1')
//剩余参数 
function buildName(firstName: string, ...restOfName: string[]) {
    return console.log(firstName + " " + restOfName.join(" "));
}
buildName("Joseph", "Samuel", "Lucas", "MacKinzie")
// 实现一个函数重载
let suits = ["hearts", "spades", "clubs", "diamonds"];

function pickCard(x: {suit: string; card: number; }[]): number;
function pickCard(x: number): {suit: string; card: number; };
function pickCard(x:any): any {
    if (typeof x == "object") {
        let pickedCard = Math.floor(Math.random() * x.length);
        return pickedCard;
    }
    else if (typeof x == "number") {
        let pickedSuit = Math.floor(x / 13);
        return { suit: suits[pickedSuit], card: x % 13 };
    }
}

let myDeck = [{ suit: "diamonds", card: 2 }, { suit: "spades", card: 10 }, { suit: "hearts", card: 4 }];
let pickedCard1 = myDeck[pickCard(myDeck)];
console.log("card: " + pickedCard1.card + " of " + pickedCard1.suit);  //card: 4 of hearts

let pickedCard2 = pickCard(15);
console.log("card: " + pickedCard2.card + " of " + pickedCard2.suit);   // card: 2 of spades

//•	在 ts 中定义一个类并实现继承
class Animal {
    move(distanceInMeters: number = 0) {
        console.log(`Animal moved ${distanceInMeters}m.`);
    }
}

class Dog extends Animal {
    bark() {
        console.log('Woof! Woof!');
    }
}

const dog = new Dog();
dog.bark();
dog.move(10);
dog.bark();

